<template><div><h2 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h2>
<p>With resource and controller classes ready, you can access the resources using the URL like <code v-pre>http://localhost/index.php?r=user/create</code>, similar to what you can do with normal Web applications.</p>
<p>In practice, you usually want to enable pretty URLs and take advantage of HTTP verbs. For example, a request <code v-pre>POST /users</code> would mean accessing the <code v-pre>user/create</code> action. This can be done easily by configuring the <code v-pre>urlManager</code> application component in the application configuration like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'urlManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'enablePrettyUrl'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'enableStrictParsing'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'showScriptName'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'rules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Compared to the URL management for Web applications, the main new thing above is the use of [[yii\rest\UrlRule]] for routing RESTful API requests. This special URL rule class will create a whole set of child URL rules to support routing and URL creation for the specified controller(s). For example, the above code is roughly equivalent to the following rules:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'PUT,PATCH users/&lt;id>'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/update'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'DELETE users/&lt;id>'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/delete'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'GET,HEAD users/&lt;id>'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/view'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'POST users'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/create'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'GET,HEAD users'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/index'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'users/&lt;id>'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/options'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'users'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user/options'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>And the following API endpoints are supported by this rule:</p>
<ul>
<li><code v-pre>GET /users</code>: list all users page by page;</li>
<li><code v-pre>HEAD /users</code>: show the overview information of user listing;</li>
<li><code v-pre>POST /users</code>: create a new user;</li>
<li><code v-pre>GET /users/123</code>: return the details of the user 123;</li>
<li><code v-pre>HEAD /users/123</code>: show the overview information of user 123;</li>
<li><code v-pre>PATCH /users/123</code> and <code v-pre>PUT /users/123</code>: update the user 123;</li>
<li><code v-pre>DELETE /users/123</code>: delete the user 123;</li>
<li><code v-pre>OPTIONS /users</code>: show the supported verbs regarding endpoint <code v-pre>/users</code>;</li>
<li><code v-pre>OPTIONS /users/123</code>: show the supported verbs regarding endpoint <code v-pre>/users/123</code>.</li>
</ul>
<p>You may configure the <code v-pre>only</code> and <code v-pre>except</code> options to explicitly list which actions to support or which actions should be disabled, respectively. For example,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'except'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'delete'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'create'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'update'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may also configure <code v-pre>patterns</code> or <code v-pre>extraPatterns</code> to redefine existing patterns or add new patterns supported by this rule. For example, to support a new action <code v-pre>search</code> by the endpoint <code v-pre>GET /users/search</code>, configure the <code v-pre>extraPatterns</code> option as follows,</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'extraPatterns'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'GET search'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'search'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You may have noticed that the controller ID <code v-pre>user</code> appears in plural form as <code v-pre>users</code> in the endpoint URLs. This is because [[yii\rest\UrlRule]] automatically pluralizes controller IDs when creating child URL rules. You may disable this behavior by setting [[yii\rest\UrlRule::pluralize]] to be <code v-pre>false</code>.</p>
<blockquote>
<p>Info: The pluralization of controller IDs is done by [[yii\helpers\Inflector::pluralize()]]. The method respects special pluralization rules. For example, the word <code v-pre>box</code> will be pluralized as <code v-pre>boxes</code> instead of <code v-pre>boxs</code>.</p>
</blockquote>
<p>In case when the automatic pluralization does not meet your requirement, you may also configure the [[yii\rest\UrlRule::controller]] property to explicitly specify how to map a name used in endpoint URLs to a controller ID. For example, the following code maps the name <code v-pre>u</code> to the controller ID <code v-pre>user</code>.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'u'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="所含规则的额外配置" tabindex="-1"><a class="header-anchor" href="#所含规则的额外配置"><span>所含规则的额外配置</span></a></h2>
<p>It could be useful to specify extra configuration that is applied to each rule contained within [[yii\rest\UrlRule]]. A good example would be specifying defaults for <code v-pre>expand</code> parameter:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'ruleConfig'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'defaults'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'expand'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


